package com.lungle.warmdiary.app.view.me.adapter;

import android.content.Context;
import android.graphics.Color;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.TextView;

import com.nostra13.universalimageloader.core.ImageLoader;
import com.lungle.warmdiary.R;
import com.lungle.warmdiary.app.model.response.MediaAccount;
import com.lungle.warmdiary.app.view.me.view.BaseViewHolder;

import java.util.List;

public class MediaCenterAdapter extends BaseAdapter {
    private List<MediaAccount> list;
    private Context mContext;

    public MediaCenterAdapter(Context context, List<MediaAccount> list) {
        this.list = list;
        this.mContext = context;
    }

    @Override
    public int getCount() {
        return list.size();
    }

    @Override
    public Object getItem(int position) {
        return position;
    }

    @Override
    public long getItemId(int position) {
        return position;
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {

        if (convertView == null) {
            convertView = LayoutInflater.from(mContext).inflate(
                    R.layout.list_cell_media_account, parent, false);
        }

        MediaAccount item = list.get(position);

        ImageLoader.getInstance().displayImage(item.getLogo(), (ImageView) BaseViewHolder.get(convertView, R.id.iv_media_icon));

        TextView name = BaseViewHolder.get(convertView, R.id.tv_media_name);
        TextView account = BaseViewHolder.get(convertView, R.id.tv_media_account);
        TextView state = BaseViewHolder.get(convertView, R.id.tv_account_state);

        name.setText(item.getName());
        account.setText(item.getUsername());
        setBackground(state, item.getStatus());


        return convertView;

    }

    private void setBackground(TextView view, int state) {
        switch (state) {
            case MediaAccount.STATE_ACTIVE:
                view.setBackgroundColor(Color.GREEN);
                view.setText("已通过");
                break;

            case MediaAccount.STATE_REGECTED:
                view.setBackgroundColor(Color.RED);
                view.setText("未通过");
                break;
            case MediaAccount.STATE_NEW:
                view.setBackgroundColor(mContext.getResources().getColor(R.color.orange));
                view.setText("待审核");
                break;
            case MediaAccount.STATE_INACTIVE:
                view.setBackgroundColor(Color.RED);
                view.setText("封禁");
                break;
        }
    }


    static class ViewHolder {
        public TextView name, from, time, action;
        public ImageView icon;

        public ViewHolder(View view) {
            name = (TextView) view.findViewById(R.id.tv_media_name);
            from = (TextView) view.findViewById(R.id.tv_media_account);
            icon = (ImageView) view.findViewById(R.id.iv_media_icon);
            time = (TextView) view.findViewById(R.id.tv_account_state);
        }
    }


}
